package com.itlong.cloud.controller.sdkV3.qrcode;

import com.fasterxml.jackson.core.type.TypeReference;
import com.itlong.cloud.POJO.DTO.sdk.QrCodeSDKDTO;
import com.itlong.cloud.POJO.DTO.sdk.QrcodeSDKGetPassRecordDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.SDKMessageVO;
import com.itlong.cloud.POJO.VO.sdk.QrcodeSDKGetPassRecordVO;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.QrcodeSDKErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.project.sdk.IQrCodeSDKServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.encrypt.Md5EncryptUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      二维码SDK 控制器
 * </desc>
 *
 * @createDate 2019/09/24
 */
@RestController
@RequestMapping("/sdkv3/cloud/qrcode/server")
public class SDKQrCodeController {
    @Autowired
    IQrCodeSDKServiceClient iQrCodeSDKServiceClient;

    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;
    /**
     * <desc>
     *      生成二维码
     * </desc>
     *
     * @param qrCode
     * @return
     * @author Jiaqi.X
     * @createDate 2018/05/24
     */
    @UnAuthAccessRequired
    @RequestMapping("secretQrCode")
    @ResponseBody
    public Object secretQrCode(String qrCode,String appid,String appsecret,String projectId,String versionNum) {
        try {
            if ("1.2".equals(versionNum)){
                Map<String, Object> objectMap = iOperateProjectServiceClient.getRobotGoProjectWhitelistAndDeviceAuthStatusByUserId(projectId, appid);
                String onlineQrCodeApi = objectMap.get("onlineQrCodeApi")+"";
                if ("0".equals(onlineQrCodeApi)){
                    return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_API_PARAM.getErrorCode());
                }
            }
            QrCodeSDKDTO qrCodeSDKDTO;
            try {
                qrCodeSDKDTO = JsonUtil.decode(qrCode, new TypeReference<QrCodeSDKDTO>() {});
            }catch (Exception e){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            if(!qrCodeSDKDTO.getUserId().matches("[0-9]{8}")){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_USER_ID_ERROR.getErrorCode());
            }else if(!qrCodeSDKDTO.getBeginDate().matches("[0-9]{10}")){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_BEGINDATE_ERROR.getErrorCode());
            }else if(!qrCodeSDKDTO.getValidityDuration().matches("[0-9]*")|| ! (Integer.valueOf(qrCodeSDKDTO.getValidityDuration()) <= 1440)){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_VALIDITYDURATION_ERROR.getErrorCode());
            }else if(!qrCodeSDKDTO.getPermissCount().matches("[0-9]*")||! (Integer.valueOf(qrCodeSDKDTO.getPermissCount()) <= 255)){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_PERMISSION_COUNT_ERROR.getErrorCode());
            }
            //校验身份信息
            if(iQrCodeSDKServiceClient.checkRequestIdentity(appid,appsecret,
                    projectId) == 0){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_IDENTITY_ERROR.getErrorCode());
            }
            Map<String,Object> result = iQrCodeSDKServiceClient.secretQrCodeStr(qrCode);
            if(result.get("secretQrcode").toString().equals("1")){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_TYPE_ERROR.getErrorCode());
            }else if(result.get("secretQrcode").toString().equals("2")){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_PROJECT_ERROR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        } catch (Exception e) {
            throw new DataAccessException("【二维码SDK_服务端_生成二维码】生成二维码异常", e);
        }
    }

    /**
     * <desc>
     *      获取通行记录
     * </desc>
     *
     * @param dto
     * @return
     * @author Jiaqi.X
     * @createDate 2019/06/30
     */
    @UnAuthAccessRequired
    public Object getPassRecord(QrcodeSDKGetPassRecordDTO dto) throws Exception {
        if(dto.getDeviceType()==null){
            return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
        }
        if ("1.2".equals(dto.getVersionNum())){
            Map<String, Object> objectMap = iOperateProjectServiceClient.getRobotGoProjectWhitelistAndDeviceAuthStatusByUserId(dto.getProjectId(),dto.getUserId());
            String onlineQrCodeApi = objectMap.get("onlineQrCodeApi")+"";
            if ("0".equals(onlineQrCodeApi)){
                return new MessageVO(QrcodeSDKErrorCodeEnum.QRCODE_API_PARAM.getErrorCode());
            }
        }
        return new SDKMessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), dto.getRequestId(), dto.getResponseTimestamp(),
                Md5EncryptUtil.getSDKV2Md5(new HashMap<>(), dto.getRequestId(), dto.getResponseTimestamp(), dto.getAppsecret()), iQrCodeSDKServiceClient.getPage(dto));
    }

}
